//
//  JoinViewController.m
//  Nschedule
//
//  Created by 이동민 on 13. 2. 16..
//  Copyright (c) 2013년 이동민. All rights reserved.
//

#import "JoinViewController.h"

@interface JoinViewController ()

@end

@implementation JoinViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    _MailText.delegate=self;
    _PasswordText.delegate=self;
    _NameText.delegate=self;
    
    //키보드가 나오는 노티피케이션 등록
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow) name:
     UIKeyboardDidShowNotification object:nil];
    
    //키보드가 사라지는 노티피케이션 등록
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide) name:UIKeyboardDidHideNotification object:nil];
    
    [super viewDidLoad];
	// Do any additional setup after loading the view.
}

//키보드가 나오는 노티피케이션 함수
-(void)keyboardWillShow
{
    //뷰 이동 애니메이션
    [UIView beginAnimations:@"Button_Animation" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:0.3f];
    [UIView setAnimationsEnabled:YES];
    
    //뷰,텍스트필드 이동
    _CalImage.frame = CGRectMake(20, 98-87, _CalImage.frame.size.width, _CalImage.frame.size.height);
    _NameText.frame = CGRectMake(63, 147-87, _NameText.frame.size.width, _NameText.frame.size.height);
    _MailText.frame = CGRectMake(63, 180-87, _MailText.frame.size.width, _MailText.frame.size.height);
    _PasswordText.frame = CGRectMake(63, 213-87, _PasswordText.frame.size.width, _PasswordText.frame.size.height);
    _CancelButton.frame = CGRectMake(63, 243-87, _CancelButton.frame.size.width, _CancelButton.frame.size.height);
    _JoinButton.frame = CGRectMake(164, 243-87, _JoinButton.frame.size.width, _JoinButton.frame.size.height);
    
    //애니메이션 시작
    [UIView commitAnimations];
    
}
//키보드가 사라지는 노티피케이션 함수
-(void)keyboardWillHide
{
    //뷰 이동 애니메이션
    [UIView beginAnimations:@"Button_Animation" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:0.3f];
    [UIView setAnimationsEnabled:YES];
    
    //뷰,텍스트필드 이동
    _CalImage.frame = CGRectMake(20, 98, _CalImage.frame.size.width, _CalImage.frame.size.height);
    _NameText.frame = CGRectMake(63, 147, _NameText.frame.size.width, _NameText.frame.size.height);
    _MailText.frame = CGRectMake(63, 180, _MailText.frame.size.width, _MailText.frame.size.height);
    _PasswordText.frame = CGRectMake(63, 213, _PasswordText.frame.size.width, _PasswordText.frame.size.height);
    _CancelButton.frame = CGRectMake(63, 243, _CancelButton.frame.size.width, _CancelButton.frame.size.height);
    _JoinButton.frame = CGRectMake(164, 243, _JoinButton.frame.size.width, _JoinButton.frame.size.height);
    
    //애니메이션 시작
    [UIView commitAnimations];
    
}


//키보드 리턴키를 터치했을때
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    //키패드가 화면에서 사라지게 됩니다.
    [textField resignFirstResponder];
    return YES;
}

//뷰를 터치하면 호출되는 메서드
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //키패트가 화면에서 사라지게 됩니다.
    [_NameText resignFirstResponder];
    [_MailText resignFirstResponder];
    [_PasswordText resignFirstResponder];
    
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)CancelButton:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


//회원가입 버튼
- (IBAction)JoinButton:(id)sender
{
//    _NameText.text
//    _MailText.text
//    _PasswordText.text
    
    NSString *sha_password = [NSString stringWithString:_PasswordText.text];
    
    
    //
    NSURL *url = [[NSURL alloc] initWithString:@"http://zzalbook.cafe24.com/nschedule/reg.php"];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    [request setHTTPMethod:@"POST"];
	
    NSString *paramDataString =
    [NSString stringWithFormat:
     @"user_email=%@&user_name=%@&user_password=%@", _MailText.text, _NameText.text, sha_password];
    NSData *paramData = [paramDataString dataUsingEncoding:NSUTF8StringEncoding];
    
    [request setHTTPBody:paramData];
    
    
    //
    NSHTTPURLResponse* urlResponse = nil;
	NSError *error = [[NSError alloc] init];
	NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
	NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
	NSLog(@"Response Code: %d", [urlResponse statusCode]);
	if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
		NSLog(@"Response: %@", result);
    }
    else {
        // 연결 실패
    }
}


@end
